/**
 * Project: complex.quartz
 * 
 * File Created at 2010-6-29
 * $Id$
 * 
 * Copyright 2008 Alibaba.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.complex.quartz;

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @description:一个简单的使用quartz触发任务的实例
 * @author gaoliang
 * @date 2010-6-29
 * @version 1.0
 */
public class SimpleTriggerRunner {
    public static void main(String[] args) {
        //创建一个JobDetail实例，指定SimpleJob
        JobDetail jobDetail = new JobDetail("job1_1", "jGroup1", SimpleJob.class);
        //通过SimpleTrigger定义调度规则：马上启动，每2秒运行一次，共运行5次
        SimpleTrigger simpleTrigger = new SimpleTrigger("trigger1_1", "tgroup1");
        simpleTrigger.setStartTime(new Date());
        simpleTrigger.setRepeatInterval(2000);
        simpleTrigger.setRepeatCount(5);
        //通过SchedulerFactory获取一个调度器实例
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            //注册并进行调度
            scheduler.scheduleJob(jobDetail, simpleTrigger);
            //调度启动
            scheduler.start();
        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /** 关联jobdetail 和 trigger的另一种方式 */
        //        //指定关联的Job组名
        //        simpleTrigger.setGroup("jGroup1");
        //        //指定关联的Job名称
        //        simpleTrigger.setJobName("job1_1");
        //        try {
        //            Scheduler scheduler = schedulerFactory.getScheduler();
        //            //注册JobDetail
        //            scheduler.addJob(jobDetail, true);
        //            //注册指定了关联JobDetail的Trigger
        //            scheduler.scheduleJob(simpleTrigger);
        //            scheduler.start();
        //        } catch (SchedulerException e) {
        //            // TODO Auto-generated catch block
        //            e.printStackTrace();
        //        }

    }
}
